﻿using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using OgilvyOne.IDAL;
using OgilvyOne.Model;

namespace OgilvyOne.SqlServerDAL
{
    public class Column : IColumn
    {
        private const string ADD_SQL = "insert into cms_column (classname,isshow,addtime,isauditing,ECLASSNAME,PARENTID,ORDERID,COLUMNTYPE,LAYERID,CLASSPIC,TEMPLATEINDEX,TEMPLATEDETAIL,DETAIL,Columntitle,ColumnKeyword,ColumnDesc,ColumnFileName,isstaticpub,CLASS_IS_STATIC,IS_INDEX,HTML_TAGS,IS_CLASS_INDEX) values(@classname,@isshow,getDate(),@isauditing,@ECLASSNAME,@PARENTID,@ORDERID,@COLUMNTYPE,@LAYERID,@CLASSPIC,@TEMPLATEINDEX,@TEMPLATEDETAIL,@DETAIL,@Columntitle,@ColumnKeyword,@ColumnDesc,@ColumnFileName,@isstaticpub,@CLASS_IS_STATIC,@IS_INDEX,@HTML_TAGS,@IS_CLASS_INDEX);select @@IDENTITY";
        /// <summary>
        /// 添加新栏目
        /// </summary>
        /// <param name="column"></param>
        /// <returns></returns>
        public bool addColumn(ColumnModel column)
        {
            bool re = false;
            SqlParameter[] parms = new SqlParameter[] { 
                new SqlParameter("@classname" , SqlDbType.VarChar, 50),
                new SqlParameter("@isshow", SqlDbType.Int, 4),
                new SqlParameter("@isauditing", SqlDbType.Int, 4),
                new SqlParameter("@ECLASSNAME", SqlDbType.VarChar, 50),
                new SqlParameter("@PARENTID", SqlDbType.Int, 4),
                new SqlParameter("@ORDERID", SqlDbType.Int, 4),
                new SqlParameter("@COLUMNTYPE",SqlDbType.Int,4),
                new SqlParameter("@LAYERID", SqlDbType.VarChar, 255),
                new SqlParameter("@CLASSPIC", SqlDbType.VarChar, 255),
                new SqlParameter("@TEMPLATEINDEX", SqlDbType.Int, 4),
                new SqlParameter("@TEMPLATEDETAIL", SqlDbType.Int, 4),
                new SqlParameter("@DETAIL", SqlDbType.VarChar, 1000),
                new SqlParameter("@Columntitle", SqlDbType.VarChar, 200),
                new SqlParameter("@ColumnKeyword", SqlDbType.VarChar, 500),
                new SqlParameter("@ColumnDesc", SqlDbType.VarChar, 500),
                new SqlParameter("@ColumnFileName", SqlDbType.VarChar , 100),
                new SqlParameter("@isstaticpub", SqlDbType.Int, 4),
                new SqlParameter("@CLASS_IS_STATIC", SqlDbType.Int, 4),
                new SqlParameter("@IS_INDEX", SqlDbType.Int, 4),
                new SqlParameter("@HTML_TAGS", SqlDbType.VarChar, 100),
                new SqlParameter("@IS_CLASS_INDEX", SqlDbType.Int, 4)
            };
            parms[0].Value = column.Classname;
            parms[1].Value = column.Isshow;
            parms[2].Value = column.Isauditing;
            parms[3].Value = column.Eclassname;
            parms[4].Value = column.Parentid;
            parms[5].Value = column.Orderid;
            parms[6].Value = column.Columntype;
            parms[7].Value = column.Layerid;
            parms[8].Value = column.Classpic;
            parms[9].Value = column.Templateindex;
            parms[10].Value = column.Templatedetail;
            parms[11].Value = column.Detail;
            parms[12].Value = column.Columntitle;
            parms[13].Value = column.Columnkeyword;
            parms[14].Value = column.Columndesc;
            parms[15].Value = column.Columnfilename;
            parms[16].Value = column.Isstaticpub;
            parms[17].Value = column.Class_is_static;
            parms[18].Value = column.Is_index;
            parms[19].Value = column.Html_tags;
            parms[20].Value = column.Is_class_index;



            using (SqlConnection conn = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        string s = SQLHelper.ExecuteScalar(trans, CommandType.Text, ADD_SQL, parms).ToString();

                        int id = int.Parse(s);    
                        if (id > 0)
                        {
                            ColumnModel pcm = getColumn(column.Parentid);
                            string upsql = "update cms_column set LAYERID='" + (pcm.Layerid + "," + id) + "' where classid=" + id;
                            SQLHelper.ExecuteNonQuery(trans, CommandType.Text, upsql, new SqlParameter[] { });
                            re = true;
                        }
                        if (re == true) trans.Commit();
                        else trans.Rollback();
                            
                       
                    }
                    catch
                    {
                        trans.Rollback();
                        throw;
                    }
                }
            }

            return re;
        }

        private const string MOD_SQL = "update cms_column set classname=@classname,isshow=@isshow,isauditing=@isauditing,ECLASSNAME=@ECLASSNAME,COLUMNTYPE=@COLUMNTYPE,CLASSPIC=@CLASSPIC,TEMPLATEINDEX=@TEMPLATEINDEX,TEMPLATEDETAIL=@TEMPLATEDETAIL,DETAIL=@DETAIL,Columntitle=@Columntitle,ColumnKeyword=@ColumnKeyword,ColumnDesc=@ColumnDesc,ColumnFileName=@ColumnFileName,isstaticpub=@isstaticpub,CLASS_IS_STATIC=@CLASS_IS_STATIC,IS_INDEX=@IS_INDEX,HTML_TAGS=@HTML_TAGS,IS_CLASS_INDEX=@IS_CLASS_INDEX where classid=@classid";
        /// <summary>
        /// 修改栏目
        /// </summary>
        /// <param name="column"></param>
        /// <returns></returns>
        public bool modColumn(ColumnModel column)
        {
            SqlParameter[] parms = new SqlParameter[] { 
                new SqlParameter("@classname" , SqlDbType.VarChar, 50),
                new SqlParameter("@isshow", SqlDbType.Int, 4),
                new SqlParameter("@isauditing", SqlDbType.Int, 4),
                new SqlParameter("@ECLASSNAME", SqlDbType.VarChar, 50),
                new SqlParameter("@COLUMNTYPE",SqlDbType.Int,4),
                new SqlParameter("@CLASSPIC", SqlDbType.VarChar, 255),
                new SqlParameter("@TEMPLATEINDEX", SqlDbType.Int, 4),
                new SqlParameter("@TEMPLATEDETAIL", SqlDbType.Int, 4),
                new SqlParameter("@DETAIL", SqlDbType.VarChar, 1000),
                new SqlParameter("@Columntitle", SqlDbType.VarChar, 200),
                new SqlParameter("@ColumnKeyword", SqlDbType.VarChar, 500),
                new SqlParameter("@ColumnDesc", SqlDbType.VarChar, 500),
                new SqlParameter("@ColumnFileName", SqlDbType.VarChar , 100),
                new SqlParameter("@isstaticpub", SqlDbType.Int, 4),
                new SqlParameter("@CLASS_IS_STATIC", SqlDbType.Int, 4),
                new SqlParameter("@IS_INDEX", SqlDbType.Int, 4),
                new SqlParameter("@HTML_TAGS", SqlDbType.VarChar, 100),
                new SqlParameter("@IS_CLASS_INDEX", SqlDbType.Int, 4),
                new SqlParameter("@classid", SqlDbType.Int, 4)
            };
            parms[0].Value = column.Classname;
            parms[1].Value = column.Isshow;
            parms[2].Value = column.Isauditing;
            parms[3].Value = column.Eclassname;
            parms[4].Value = column.Columntype;
            
            parms[5].Value = column.Classpic;
            parms[6].Value = column.Templateindex;
            parms[7].Value = column.Templatedetail;
            parms[8].Value = column.Detail;
            parms[9].Value = column.Columntitle;
            parms[10].Value = column.Columnkeyword;
            parms[11].Value = column.Columndesc;
            parms[12].Value = column.Columnfilename;
            parms[13].Value = column.Isstaticpub;
            parms[14].Value = column.Class_is_static;
            parms[15].Value = column.Is_index;
            parms[16].Value = column.Html_tags;
            parms[17].Value = column.Is_class_index;
            parms[18].Value = column.Classid;
            int i = SQLHelper.ExecuteSql(MOD_SQL, parms);
            if (i > 0) return true;
            return false;
        }
        /// <summary>
        /// 英文栏目名是否已存在
        /// </summary>
        /// <param name="name"></param>
        /// <param name="type">类型,1英文,0中文</param>
        /// <returns></returns>
        public bool IsExistsEnclass(string name, int type)
        {
            string sql = "select classid from cms_column";
            if (type == 0)
            {
                sql += " where classname =@name";
            }
            else
            {
                sql += " where eclassname =@name";
            }
            SqlParameter []parms = new SqlParameter[]{
                new SqlParameter("@name",SqlDbType.VarChar,50)
            };
            parms[0].Value = name;
            bool re = false;
            using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, COLUMN_SQL, parms))
            {

                if (rdr.Read())
                {
                    re = true;
                }
            }

            return re;
        }

        private const string MAX_ORDER_SQL = "select max(ORDERID) from cms_column c where c.PARENTID=@cid ";
        /// <summary>
        /// 子栏目最大排序值
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        public int MaxOrder(int pid)
        {
            int oid = 0;
            SqlParameter[] parms = new SqlParameter[] { 
                new SqlParameter("@cid", SqlDbType.Int, 4)
            };
            parms[0].Value = pid;
            using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, MAX_ORDER_SQL, parms))
            {
                if (rdr.Read())
                {
                   if(!rdr.IsDBNull(0))
                   {
                       oid = rdr.GetInt32(0);
                   }
                }
            }
            return oid;
        }

        /// <summary>
        /// 删除栏目
        /// </summary>
        /// <param name="cid">栏目ID</param>
        /// <returns></returns>
        public bool delColumn(int cid)
        {
            IList plist = getColumnList(cid);
            if (plist.Count > 0)
            {
                return false;
            }
            string delsql = "delete from cms_column where classid=" + cid;
            int i = SQLHelper.ExecuteSql(delsql, new SqlParameter[] { });
            if (i > 0) return true;
            return false;
        }

        private const string COLUMN_SQL = "select c.classid,c.classname,c.isshow,c.addtime,isauditing,ECLASSNAME,PARENTID,ORDERID,COLUMNTYPE,LAYERID,CLASSPIC,TEMPLATEINDEX,TEMPLATEDETAIL,DETAIL,Columntitle,ColumnKeyword,ColumnDesc,ColumnFileName,isstaticpub,CLASS_IS_STATIC,IS_INDEX,HTML_TAGS,IS_CLASS_INDEX from cms_column c where c.classid=@cid ";
        /// <summary>
        /// 获得栏目信息
        /// </summary>
        /// <param name="cid">栏目ID</param>
        /// <returns></returns>
        public ColumnModel getColumn(int cid)
        {
            ColumnModel uc = new ColumnModel();
            SqlParameter[] parms = new SqlParameter[] { 
                new SqlParameter("@cid", SqlDbType.Int, 4)
            };
            parms[0].Value = cid;
            using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, COLUMN_SQL, parms))
            {
                if (rdr.Read())
                {
                    
                    uc.Classid = rdr.GetInt32(0);
                    uc.Classname = rdr.GetString(1);
                    uc.Isshow = (int)rdr.GetInt32(2);
                    if (!rdr.IsDBNull(3))
                        uc.Addtime = rdr.GetDateTime(3);
                    if (!rdr.IsDBNull(4))
                        uc.Isauditing = rdr.GetInt32(4);
                    if (!rdr.IsDBNull(5))
                    {
                        uc.Eclassname = rdr.GetString(5);
                    }
                    if (!rdr.IsDBNull(6))
                    {
                        uc.Parentid = rdr.GetInt32(6);
                    }
                    if (!rdr.IsDBNull(7))
                    {
                        uc.Orderid = rdr.GetInt32(7);
                    }
                    if (!rdr.IsDBNull(8))
                    {
                        uc.Columntype = rdr.GetInt32(8);
                    }
                    if (!rdr.IsDBNull(9))
                    {
                        uc.Layerid = rdr.GetString(9);
                    }
                    if (!rdr.IsDBNull(10))
                    {
                        uc.Classpic = rdr.GetString(10);
                    }
                    if (!rdr.IsDBNull(11))
                    {
                        uc.Templateindex = rdr.GetInt32(11);
                    }
                    if (!rdr.IsDBNull(12))
                    {
                        uc.Templatedetail = rdr.GetInt32(12);
                    }
                    if (!rdr.IsDBNull(13))
                    {
                        uc.Detail = rdr.GetString(13);
                    }
                    if (!rdr.IsDBNull(14))
                    {
                        uc.Columntitle = rdr.GetString(14);
                    }
                    if (!rdr.IsDBNull(15))
                    {
                        uc.Columnkeyword = rdr.GetString(15);
                    }
                    if (!rdr.IsDBNull(16))
                    {
                        uc.Columndesc = rdr.GetString(16);
                    }
                    if (!rdr.IsDBNull(17))
                    {
                        uc.Columnfilename = rdr.GetString(17);
                    }
                    if (!rdr.IsDBNull(18))
                    {
                        uc.Isstaticpub = rdr.GetInt32(18);
                    }
                    if (!rdr.IsDBNull(19))
                    {
                        uc.Class_is_static = rdr.GetInt32(19);
                    }
                    if (!rdr.IsDBNull(20))
                    {
                        uc.Is_index = rdr.GetInt32(20);
                    }
                    if (!rdr.IsDBNull(21))
                    {
                        uc.Html_tags = rdr.GetString(21);
                    }
                    if (!rdr.IsDBNull(22))
                    {
                        uc.Is_class_index = rdr.GetInt32(22);
                    }
                    
                }
            }
            return uc;
        }

        private const string COLUMN_ALL_LIST_SQL = "select c.classid,c.classname,c.isshow,c.addtime,isauditing,ECLASSNAME,PARENTID,ORDERID,COLUMNTYPE,LAYERID,CLASSPIC,TEMPLATEINDEX,TEMPLATEDETAIL,DETAIL,Columntitle,ColumnKeyword,ColumnDesc,ColumnFileName,isstaticpub,CLASS_IS_STATIC,IS_INDEX,HTML_TAGS,IS_CLASS_INDEX from cms_column c order by parentid, c.orderid ";
        /// <summary>
        /// 获得所有栏目信息
        /// </summary>
        /// <returns></returns>
        public IList getColumnAllList()
        {
            IList datalist = new ArrayList();
            SqlParameter[] parms = new SqlParameter[] { };
            
            using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, COLUMN_ALL_LIST_SQL, parms))
            {
                while (rdr.Read())
                {
                    ColumnModel uc = new ColumnModel();
                    uc.Classid = rdr.GetInt32(0);
                    uc.Classname = rdr.GetString(1);
                    uc.Isshow = (int)rdr.GetInt32(2);
                    if (!rdr.IsDBNull(3))
                        uc.Addtime = rdr.GetDateTime(3);
                    if (!rdr.IsDBNull(4))
                        uc.Isauditing = rdr.GetInt32(4);
                    if (!rdr.IsDBNull(5))
                    {
                        uc.Eclassname = rdr.GetString(5);
                    }
                    if (!rdr.IsDBNull(6))
                    {
                        uc.Parentid = rdr.GetInt32(6);
                    }
                    if (!rdr.IsDBNull(7))
                    {
                        uc.Orderid = rdr.GetInt32(7);
                    }
                    if (!rdr.IsDBNull(8))
                    {
                        uc.Columntype = rdr.GetInt32(8);
                    }
                    if (!rdr.IsDBNull(9))
                    {
                        uc.Layerid = rdr.GetString(9);
                    }
                    if (!rdr.IsDBNull(10))
                    {
                        uc.Classpic = rdr.GetString(10);
                    }
                    if (!rdr.IsDBNull(11))
                    {
                        uc.Templateindex = rdr.GetInt32(11);
                    }
                    if (!rdr.IsDBNull(12))
                    {
                        uc.Templatedetail = rdr.GetInt32(12);
                    }
                    if (!rdr.IsDBNull(13))
                    {
                        uc.Detail = rdr.GetString(13);
                    }
                    if (!rdr.IsDBNull(14))
                    {
                        uc.Columntitle = rdr.GetString(14);
                    }
                    if (!rdr.IsDBNull(15))
                    {
                        uc.Columnkeyword = rdr.GetString(15);
                    }
                    if (!rdr.IsDBNull(16))
                    {
                        uc.Columndesc = rdr.GetString(16);
                    }
                    if (!rdr.IsDBNull(17))
                    {
                        uc.Columnfilename = rdr.GetString(17);
                    }
                    if (!rdr.IsDBNull(18))
                    {
                        uc.Isstaticpub = rdr.GetInt32(18);
                    }
                    if (!rdr.IsDBNull(19))
                    {
                        uc.Class_is_static = rdr.GetInt32(19);
                    }
                    if (!rdr.IsDBNull(20))
                    {
                        uc.Is_index = rdr.GetInt32(20);
                    }
                    if (!rdr.IsDBNull(21))
                    {
                        uc.Html_tags = rdr.GetString(21);
                    }
                    if (!rdr.IsDBNull(22))
                    {
                        uc.Is_class_index = rdr.GetInt32(22);
                    }

                    datalist.Add(uc);

                }
            }
            return datalist;
        }

        private const string COLUMN_LIST_SQL = "select c.classid,c.classname,c.isshow,c.addtime,isauditing,ctname,isstaticpub from cms_column c left join cms_column_type ct on c.columntype=ct.columntypeid where c.parentid=@cid order by orderid";
        /// <summary>
        /// 获取子栏目列表
        /// </summary>
        /// <param name="pcid">父ID</param>
        /// <returns></returns>
        public IList getColumnList(int pcid)
        {
            IList datalist = new ArrayList();
            SqlParameter[] parms = new SqlParameter[] { 
                new SqlParameter("@cid", SqlDbType.Int, 4)
            };
            parms[0].Value = pcid;
            using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, COLUMN_LIST_SQL, parms))
            {
                while (rdr.Read())
                {
                    ColumnModel uc = new ColumnModel();
                    uc.Classid = rdr.GetInt32(0);
                    if (!rdr.IsDBNull(1))
                        uc.Classname = rdr.GetString(1);
                    uc.Isshow = rdr.GetInt32(2);
                    if(!rdr.IsDBNull(3))
                        uc.Addtime = rdr.GetDateTime(3);
                    if (!rdr.IsDBNull(4))
                        uc.Isauditing = rdr.GetInt32(4);
                    if (!rdr.IsDBNull(5))
                    {
                        uc.Ctname = rdr.GetString(5);
                    }
                    if (!rdr.IsDBNull(6))
                    {
                        uc.Isstaticpub = rdr.GetInt32(6);
                    }
                    datalist.Add(uc);
                }
            }
            return datalist;
        }

        
        /// <summary>
        /// 获取所有子栏目
        /// </summary>
        /// <param name="layerid">层级ID</param>
        /// <returns></returns>
        public IList getColumnListAll(string layerid)
        {
            string COLUMN_LIST_ALL_SQL = "select c.classid,c.classname,c.isshow,c.addtime,isauditing,ECLASSNAME,PARENTID,ORDERID,COLUMNTYPE,LAYERID,CLASSPIC,TEMPLATEINDEX,TEMPLATEDETAIL,DETAIL,Columntitle,ColumnKeyword,ColumnDesc,ColumnFileName,isstaticpub from cms_column c ";
            COLUMN_LIST_ALL_SQL += " where c.LAYERID like '" + layerid + "%'";

            IList datalist = new ArrayList();
            SqlParameter[] parms = new SqlParameter[] { };
            using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, COLUMN_LIST_ALL_SQL, parms))
            {
                while (rdr.Read())
                {
                    ColumnModel uc = new ColumnModel();
                    uc.Classid = rdr.GetInt32(0);
                    uc.Classname = rdr.GetString(1);
                    uc.Isshow = rdr.GetInt32(2);
                    if (!rdr.IsDBNull(3))
                        uc.Addtime = rdr.GetDateTime(3);
                    if (!rdr.IsDBNull(4))
                        uc.Isauditing = rdr.GetInt32(4);
                    if (!rdr.IsDBNull(5))
                    {
                        uc.Eclassname = rdr.GetString(5);
                    }
                    if (!rdr.IsDBNull(6))
                    {
                        uc.Parentid = rdr.GetInt32(6);
                    }
                    if (!rdr.IsDBNull(7))
                    {
                        uc.Orderid = rdr.GetInt32(7);
                    }
                    if (!rdr.IsDBNull(8))
                    {
                        uc.Columntype = rdr.GetInt32(8);
                    }
                    if (!rdr.IsDBNull(9))
                    {
                        uc.Layerid = rdr.GetString(9);
                    }
                    if (!rdr.IsDBNull(10))
                    {
                        uc.Templateindex = rdr.GetInt32(10);
                    }
                    if (!rdr.IsDBNull(11))
                    {
                        uc.Templatedetail = rdr.GetInt32(12);
                    }
                    if (!rdr.IsDBNull(12))
                    {
                        uc.Detail = rdr.GetString(12);
                    }
                    if (!rdr.IsDBNull(13))
                    {
                        uc.Columntitle = rdr.GetString(13);
                    }
                    if (!rdr.IsDBNull(14))
                    {
                        uc.Columnkeyword = rdr.GetString(14);
                    }
                    if (!rdr.IsDBNull(15))
                    {
                        uc.Columndesc = rdr.GetString(15);
                    }
                    if (!rdr.IsDBNull(16))
                    {
                        uc.Columnfilename = rdr.GetString(16);
                    }
                    if (!rdr.IsDBNull(17))
                    {
                        uc.Isstaticpub = rdr.GetInt32(17);
                    }
                    datalist.Add(uc);
                }
            }

            return datalist;
        }

        /// <summary>
        /// 获取层级内容栏目
        /// </summary>
        /// <param name="layerid">层级ID</param>
        /// <returns></returns>
        public IList getColumnLayerList(string layerid)
        {

            string layersql = "select c.classid,c.classname,c.layerid,ct.backlink,parentid,isauditing,COLUMNTYPE from cms_column c left join cms_column_type ct on c.columntype=ct.columntypeid  where c.classid<> 1 and c.classid in (" + layerid+ ") order by parentid, c.orderid";
            IList datalist = new ArrayList();
            SqlParameter[] parms = new SqlParameter[] { };
            using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, layersql, parms))
            {
                while (rdr.Read())
                {
                    UserColumnModel uc = new UserColumnModel();
                    uc.Columnid = rdr.GetInt32(0);
                    if (!rdr.IsDBNull(1))
                        uc.Columnname = rdr.GetString(1);
                    if (!rdr.IsDBNull(2))
                        uc.Layerid = rdr.GetString(2);
                    if (!rdr.IsDBNull(3))
                        uc.Columnurl = rdr.GetString(3);
                    if (!rdr.IsDBNull(4))
                        uc.Parentid = rdr.GetInt32(4);
                    if (!rdr.IsDBNull(5))
                        uc.Audit = rdr.GetInt32(5).ToString();
                    uc.Columntypeid = rdr.GetInt32(6);
                    datalist.Add(uc);
                }
            }
            return datalist;
        }
        /// <summary>
        /// 获取系统栏目层级列表
        /// </summary>
        /// <param name="layerid"></param>
        /// <returns></returns>
        public IList getSysColumnLayerList(string layerid)
        {
            string layersql = "select id,classname,layerid,linkurl,parentid from cms_syscolumn where id in(" + layerid + ") order by parentid, orderid";
            IList datalist = new ArrayList();
            SqlParameter[] parms = new SqlParameter[] { };
            using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, layersql, parms))
            {
                while (rdr.Read())
                {
                    UserColumnModel uc = new UserColumnModel();
                    uc.Columnid = rdr.GetInt32(0);
                    uc.Columnname = rdr.GetString(1);
                    uc.Layerid = rdr.GetString(2);
                    uc.Columnurl = rdr.GetString(3);
                    uc.Parentid = rdr.GetInt32(4);
                    datalist.Add(uc);
                }
            }

            return datalist;
        }
        /// <summary>
        /// 获取子栏目排序号栏目ID
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="orderid"></param>
        /// <returns></returns>
        public int OrderColumn(int pid, int orderid)
        {
            int cid = 0;
            string osql = "select classid from cms_column where parentid = " + pid + " and orderid = " + orderid;
            using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, osql, new SqlParameter[]{}))
            {
                if (rdr.Read())
                {
                    cid = rdr.GetInt32(0);
                }
            }
            return cid;
        }
        /// <summary>
        /// 修改栏目排序号
        /// </summary>
        /// <param name="cid"></param>
        /// <param name="orderid"></param>
        /// <returns></returns>
        public bool ModOrderColumn(int cid, int orderid)
        {
            bool re = false;
            string usql = "update cms_column set orderid = " + orderid + " where classid=" + cid;
            int i = SQLHelper.ExecuteSql(usql, new SqlParameter[] { });
            if (i > 0) return true;
            return re;
        }

        /// <summary>
        /// 获取HTML模板标签列表
        /// </summary>
        /// <param name="layerid"></param>
        /// <returns></returns>
        public IList getHtmlTempLabelList()
        {
            string layersql = "select LABEL_ID,LABEL_NAME from CMS_TEMPLATE_LABEL where IS_HTML=1";
            IList datalist = new ArrayList();
            SqlParameter[] parms = new SqlParameter[] { };
            using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, layersql, parms))
            {
                while (rdr.Read())
                {
                    TemplateLabelModel uc = new TemplateLabelModel();
                    uc.Id = rdr.GetInt32(0);
                    uc.Name = rdr.GetString(1);
                    datalist.Add(uc);
                }
            }

            return datalist;
        }
    }
}
